package com.beamfield.controller.manage.portal;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.News;
import com.beamfield.modelVo.NewsVo;
import com.beamfield.service.manage.portal.NewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 后台门户新闻管理
 */
@RequestMapping("/cms/portal/news")
@Controller
public class NewsController extends BaseController {

    private final NewsService newsService;

    @Autowired
    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }
    @RequestMapping(value = "/news",method = RequestMethod.GET)
    public ModelAndView newsPage(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/editNews",method = RequestMethod.GET)
    public ModelAndView editNewsPage(){
        return new ModelAndView();
    }

    /**
     * 获取新闻
     * @param request
     * @return
     */
    @RequestMapping(value = "/readNewsData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readNewsData(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        String newsId = request.getParameter("news_id");
        String newsSectionId = request.getParameter("newsSectionId");
        String title = request.getParameter("title");
        List<NewsVo> newsList = newsService.getNewsData(offset, limit, beamId, newsId, title, newsSectionId);
        Long total = newsService.countNewsData(beamId, newsId, title,newsSectionId);
        Integer page = null;

        if(total == null || total==0 || StringUtils.isEmpty(offset)){
            page = 0;
        }else{
            page =  Integer.parseInt(offset)/Integer.parseInt(limit)+1;
        }
        return new PageResult(total, page, newsList);
    }
    /**
     * 保存新闻
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request, News news){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        news.setBeam_id(beamId);
        news.setOperator(userId);
        news.setDel_flag(0);
        news.setAddtime(new Date());
        news.setType(3);
        int result = 0;
        try{
            result = newsService.save(news);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"新增成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 上传文件
     * @param request
     * @return
     */
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @ResponseBody
    public Map upload(HttpServletRequest request,@RequestParam("imgFile") MultipartFile file){
            Map<String,Object> resultMap = new HashMap<>();
        if(file == null){
            resultMap.put("error", 1);
            resultMap.put("message", "上传失败");
            return resultMap;
        }
        long fileId = 0L;
        try {
            fileId = super.saveFile((CommonsMultipartFile) file);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("error", 1);
            resultMap.put("message", "上传失败");
            return resultMap;
        }
        resultMap.put("error", 0);
        resultMap.put("url", newsService.getFileUrl(fileId));
        return resultMap;
    }

    /**
     * 删除一条新闻
     * @param request
     * @return
     */
    @RequestMapping(value = "/doDelete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doDelele(HttpServletRequest request){
        Long userId = super.getUserId(request);
        String newsId = request.getParameter("news_id");
        if(StringUtils.isEmpty(newsId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{
            result = newsService.deleteOne(newsId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"删除成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 更新新闻
     * @param request
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult update(HttpServletRequest request,News news){
        Long userId = super.getUserId(request);
        Long newsId = news.getNews_id();
        if(newsId == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        news.setUpdater(userId);
        news.setUpdatetime(new Date());
        int result = 0;
        try{
            result = newsService.updateOne(news);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"更新成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

}
